package pers.cz.chat.filter;

import io.netty.channel.Channel;
import pers.cz.chat.UserManager;
import pers.cz.netty.filter.FilterHandler;
import pers.cz.netty.filter.FilterHandlerContext;
import pers.cz.netty.protocal.Message;
import pers.cz.netty.protocal.MsgType;

/**
 * @version 1.0
 * @description: 注册拦截器
 * @author: ChengZhi 0336
 * @create: 2025-01-06 18:13
 **/
public class RegistFilter implements FilterHandler<Message, Channel> {

	@Override
	public boolean approve(Message message, Channel channel, FilterHandlerContext<Message, Channel> handlerContext) {

		if (MsgType.REGIST.equals(message.getMsgType())) {

			String username = message.getAttachment("username");
			String password = message.getAttachment("password");
			UserManager.registUser(username, password);

			Message loginSuccessMessage = new Message();
			loginSuccessMessage.addAttachment("source", "server");
			loginSuccessMessage.setMsgType(MsgType.REGIST);
			loginSuccessMessage.setContent(username + "注册成功！");
			channel.writeAndFlush(loginSuccessMessage);
			return true;
		}
		return handlerContext.doFilter(message, channel);
	}
}
